package com.guangaiboen.attendance.wx.util;

import com.google.common.base.Throwables;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateFormatUtils extends org.apache.commons.lang3.time.DateFormatUtils {

    public static String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss";

    //SimpleDateFormat不能定义为一个静态变量，SimpleDateFormat是线程不安全的，不能共享，请每次使用new 一个新的
    public static SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
    //替代方案：java8
    public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DEFAULT_PATTERN);

    /**解析：推荐方式*/
    public static Date parseDate(String time) {
        return DateUtils.parseDate(time, DEFAULT_PATTERN);
    }

    public static Date parseDate(String time, String format) {
        return DateUtils.parseDate(time, format);
    }

    /**格式化：推荐方式
     * String date = DateFormatUtils.Defaulattern(new Date(), Defaulattern);*/
    public static String format(Date date) {
        if(date == null ){
            return "";
        }
        return DateFormatUtils.format(date, DEFAULT_PATTERN);
    }

    /**格式化：推荐方式 "yyyy-MM-dd HH:mm:ss"*/
    public static Date format(String dateStr) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            Throwables.throwIfUnchecked(e);
        }
        return null;
    }

    /**java8 格式化日期 线程安全*/
    public static String formatDate(LocalDateTime date){
        return formatter.format(date);
    }

    public static String formatDate(LocalDateTime date,String pattern){
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return formatter.format(date);
    }

    /**java8 str转date 线程安全 */
    public static LocalDateTime parse(String dateStr){
        return LocalDateTime.parse(dateStr,formatter);
    }


    /**获取当前时间，并根据需要格式化*/
    public static String getNowformat(String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return dateTimeFormatter.format(LocalDateTime.now());
    }

    /**获取当前时间几天，并根据需要格式化
     * i为正数时，表示获取当前时间后几天，为负数时为前几天*/
    public static String getDistanceNowByFormat(String pattern,int i){
        return LocalDateTime.now().plusDays(i).format(DateTimeFormatter.ofPattern(pattern));
    }

    /**=========================excel表格制作常用日期格式转换==================start====================**/
    public static final DecimalFormat fmt1 = new DecimalFormat("#");
    public static final SimpleDateFormat dateFmt1 = new SimpleDateFormat("yyyy/MM/dd HH");
    public static final SimpleDateFormat dateFmt2 = new SimpleDateFormat("yyyy/MM/dd");
    public static final SimpleDateFormat dateFmt3 = new SimpleDateFormat("yyyy/MM/dd HH:mm");
    public static final SimpleDateFormat dateFmt4 = new SimpleDateFormat("yyyy/MM/dd HH:mm a");
    public static final SimpleDateFormat dateFmt5 = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

    public static String formatNumber1(Number number) {
        return fmt1.format(number);
    }

    public static String formatDate(Date date, int dateType) {
        String result = null;
        switch (dateType) {
            case 178:
                result = dateFmt1.format(date);
                break;
            case 14:
                result = dateFmt2.format(date);
                break;
            case 179:
                result = dateFmt3.format(date);
                break;
            case 181:
                result = dateFmt4.format(date);
                break;
            case 22:
                result = dateFmt5.format(date);
                break;
            default:
                break;
        }
        return result;
    }
    /**=========================excel表格制作常用日期格式转换==================end====================**/

}
